package com.idea.zd.sys.util.word;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.Document;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class DocPicture {
    byte[] picData;
    int picType = Document.PICTURE_TYPE_PNG;
    int width;
    int height;

    public DocPicture(String filePath){
        try {
            detectPicInfoByFile(filePath, 0, 0, 0, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DocPicture(String filePath, int picType){
        try {
            detectPicInfoByFile(filePath, picType, 0, 0, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DocPicture(String filePath, int width, int height){
        this(filePath, width, height, false);
    }

    public DocPicture(String filePath, int width, int height, boolean keepRatio){
        try {
            detectPicInfoByFile(filePath, 0, width, height, keepRatio);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DocPicture(String filePath, int picType, int width, int height, boolean keepRatio){
        try {
            detectPicInfoByFile(filePath, picType, width, height, keepRatio);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void detectPicInfoByFile(String file, int picType, int width, int height, boolean keepRatio) throws IOException{
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            this.picData = IOUtils.toByteArray(in);
            if (picType == 0) {
                String ext = FilenameUtils.getExtension(file);
                detectPicTypeByExt(ext);
            } else {
                this.picType = picType;
            }
            IOUtils.closeQuietly(in);

            if (width == 0 || height == 0 || keepRatio) {
                in = new FileInputStream(file);
                BufferedImage bi = ImageIO.read(in);
                if (keepRatio) {
                    int w = bi.getWidth();
                    int h = bi.getHeight();
                    float ratio = (float) w / (float) h;
                    if (w > width) {
                        this.width = width;
                        this.height = Math.round(width / ratio);
                        if (this.height > height) {
                            this.height = height;
                            this.width = Math.round(height * ratio);
                        }
                    } else if (h > height) {
                        this.height = height;
                        this.width = Math.round(height * ratio);
                        if (this.width > width) {
                            this.width = width;
                            this.height = Math.round(width / ratio);
                        }
                    } else {
                        if (h > w) {
                            this.height = h;
                            this.width = Math.round(h * ratio);
                        } else {
                            this.width = w;
                            this.height = Math.round(w / ratio);
                        }
                    }
                } else {
                    this.width = bi.getWidth();
                    this.height = bi.getHeight();
                }
            } else {
                this.width = width;
                this.height = height;
            }
        }finally {
            IOUtils.closeQuietly(in);
        }
    }

    private void detectPicTypeByExt(String ext){
        if("png".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_PNG;
        else if("jpg".equalsIgnoreCase(ext) || "jpeg".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_JPEG;
        else if("bmp".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_BMP;
        else if("gif".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_GIF;
        else if("emf".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_EMF;
        else if("wmf".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_WMF;
        else if("dib".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_DIB;
        else if("tiff".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_TIFF;
        else if("eps".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_EPS;
        else if("wpg".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_WPG;
        else if("pict".equalsIgnoreCase(ext))
            this.picType = Document.PICTURE_TYPE_PICT;
    }

    public byte[] getPicData() {
        return picData;
    }

    public void setPicData(byte[] picData) {
        this.picData = picData;
    }

    public int getPicType() {
        return picType;
    }

    public void setPicType(int picType) {
        this.picType = picType;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}
